package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.QuestionUserLock;
import com.tanhua.domain.db.Questionnaire;
import com.tanhua.domain.db.SoulQuestion;
import com.tanhua.domain.db.SoulQuestionOption;
import com.tanhua.dubbo.api.SoulApi;
import com.tanhua.dubbo.mapper.QuestionUserLockMapper;
import com.tanhua.dubbo.mapper.QuestionnaireMapper;
import com.tanhua.dubbo.mapper.SoulQuestionMapper;
import com.tanhua.dubbo.mapper.SoulQuestionOptionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class SoulApiImpl implements SoulApi {


    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Autowired
    private SoulQuestionMapper soulQuestionMapper;
    @Autowired
    private SoulQuestionOptionMapper soulQuestionOptionMapper;



    @Override
    public List<Questionnaire> findQuestionnaires() {
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        List<Questionnaire> questionnaires = questionnaireMapper.selectList(null);
        return questionnaires;
    }

    @Override
    public Questionnaire findQuestionnaireById(Long id) {
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Questionnaire questionnaire = questionnaireMapper.selectOne(queryWrapper);
        return questionnaire;
    }

    @Override
    public List<SoulQuestion> findSoulQuestionByQuestionnaireId(Long questionnaireId) {
        QueryWrapper<SoulQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_id", questionnaireId);
        // 获取问卷的试题
        List<SoulQuestion> soulQuestionList = soulQuestionMapper.selectList(queryWrapper);
        return soulQuestionList;
    }

    @Override
    public SoulQuestion findSoulQuestionById(Long id) {
        QueryWrapper<SoulQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        // 获取问卷的试题
        return soulQuestionMapper.selectOne(queryWrapper);
    }

    @Override
    public List<SoulQuestionOption> findSoulQuestionOptionById(Long id) {
        QueryWrapper<SoulQuestionOption> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("question_id", id);
        // 获取问卷选项
        List<SoulQuestionOption> soulQuestionOptionList = soulQuestionOptionMapper.selectList(queryWrapper2);
       return soulQuestionOptionList;
    }

    @Override
    public Long findScoreById(Long optionId, Long questionId) {

        QueryWrapper<SoulQuestionOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", optionId).eq("question_id", questionId);

        SoulQuestionOption soulQuestionOption = soulQuestionOptionMapper.selectOne(queryWrapper);
        return soulQuestionOption.getScore();
    }
}
